#coding=utf-8
#!/usr/bin/python
# this is my first sample

'''
Created on 2012-11-17

@author: Neo.Chen
'''

#特殊参数形式的测试
# “*” 的应用 --- * 用来传递任意个无名字参数，这些参数会一个Tuple的形式访问。
def special_fun_1(*keys):
    print("keys type=%s" % type(keys))
    print("keys=%s" % str(keys))                        #keys type=<class 'tuple'>
    for i in range(0, len(keys)):
        print("keys[" + str(i) + "]=%s" % str(keys[i])) #keys[0]=2

# “**” 的应用 --- **用来处理传递任意个有名字的参数，这些参数用dict来访问
def special_fun_2(**keys):
    print("keys type=%s" % type(keys))                  #keys type=<class 'dict'>
    print("keys=%s" % str(keys))                        #keys={'name': 'vp', 'age': 19}
    print("name=%s" % str(keys['name']))                #name=vp

def special_fun_test():
    special_fun_1(2,3,4,5)
    special_fun_2(name="vp", age=19)


#02为包头的命令码定义
commandinfo = {
    0x0001 : 'qq_logout',                # 退出登陆
    0x0002 : 'qq_alive',                 # 保持自己的在线状态
    0x0003 : 'qq_reg_id_2',              # 注册新ID2
    0x0004 : 'qq_updata_info',           # 更新自己信息
    }
def command_test():
    # 通过名称查出消息ID的命令码，反转一次就可以了。
    nametoid = {}
    for k in commandinfo.keys():
        print(commandinfo[k])
        nametoid[commandinfo[k]] = k

def tuple_test():
    print("元组可以不用括号，下面是例子")
    cc = 3,2
    aa,bb = cc
    print("aa=%d, bb=%d" % (aa, bb))
    print("aa=%d, bb=%d" % cc)

def string_split_test():
    print("字符串分解")
    mytext = "http://www.sohu.com/test/vvv"
    list_text = str.split(mytext, "/")
    for item in list_text:
        print(item)

g_buffer = b"xxxx"
def global_read_test():
    print(g_buffer)

def global_write_error_test1():
    g_buffer = b"yyyyy"
    print(g_buffer)

def global_write_error_test2():
    g_buffer = g_buffer + b"yyyyy"
    print(g_buffer)

def global_write_success_test():
    global g_buffer
    g_buffer = g_buffer + b"yyyyy"
    print(g_buffer)


def str_convert_test():
    a = u'中文'
    a_gb2312 = a.encode('gb2312')
    print(a_gb2312)
    #编码1转换为编码2可以先转为unicode再转为编码2
    a_unicode = a_gb2312.decode('gb2312')
    print(a_unicode)
    a_utf8 = a_unicode.encode('utf8')
    #dos不识别utf8编码，直接输出会是乱码
    print(a_utf8)




if __name__ == '__main__':
    special_fun_test()
    command_test()
    tuple_test()
    string_split_test()
    '''
        python 对于写操作，默认是局部的，所以要写全局变量，就要加global，对于读操作，就没这个问题
    '''
    global_read_test()          #打印结果b'xxxx'
    global_write_error_test1()  #打印结果：b'yyyyy'
    global_read_test()          #打印结果b'xxxx'
    #global_write_error_test2()  #错误
    global_write_success_test()
